﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.IO;
using senac.ws.interfaces;
using senac.ws.negocio;
using senac.util;

namespace senac.ws.consultaCursosUnidade
{
    public partial class ServiceSenac : ServiceBase
    {
        Thread threadStartConsulta = null;
        
        public ServiceSenac()
        {
            InitializeComponent();
        }

        public void StartService()
        {
                while (true)
                {
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();

                    Negocio.Instance.AnalisarTabelas();

                    int tempoPassado = 0;
                    if (Int32.TryParse(stopwatch.ElapsedMilliseconds.ToString(), out tempoPassado))
                    {
                        if (tempoPassado < 600000)
                            Thread.Sleep(600000 - tempoPassado);
                    }
                }
        }

        protected override void OnStart(string[] args)
        {
            threadStartConsulta = new Thread(new ThreadStart(StartService));
            threadStartConsulta.IsBackground = true;
            Util.Instance.EscreverLog(Util.opcoesEscrita.EscreverInicio, string.Empty);
            threadStartConsulta.Start();
        }

        protected override void OnStop()
        {
            Util.Instance.EscreverLog(Util.opcoesEscrita.EscreverFim, string.Empty);
            threadStartConsulta.Abort();
        }
    }
}
